<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Empleado extends CI_Controller {

    public function __construct() {
        parent::__construct();
        
        $this->load->library('form_validation');        
    }

    /**
     * Index Page for this controller.
     *
     * Maps to the following URL
     * 		http://example.com/index.php/welcome
     * 	- or -  
     * 		http://example.com/index.php/welcome/index
     * 	- or -
     * Since this controller is set as the default controller in 
     * config/routes.php, it's displayed at http://example.com/
     *
     * So any other public methods not prefixed with an underscore will
     * map to /index.php/welcome/<method_name>
     * @see http://codeigniter.com/user_guide/general/urls.html
     */
    public function index() {
    $this->load->helper('form');    
        $datae = array(
            "ficha" => $this->input->post("Ficha"),
            "nombre" => $this->input->post("Nombre"),
            "dir_emp" => $this->input->post("Dir_Emp"),
            "est_civil" => $this->input->post("Est_Civil"),
            "sexo" => $this->input->post("Sexo"),
            "f_nac" => $this->input->post("F_Nac"),
            "f_ing" => $this->input->post("F_Ing"),
            "f_egr" => $this->input->post("F_Egr"),
            "status" => $this->input->post("Status"),
            "s_dev" => $this->input->post("S_Dev"),
            "ficha_j" => $this->input->post("Ficha_J"),
            "cod_b" => $this->input->post("Cod_B"),            
            "cod_s" => $this->input->post("Cod_S"),
            "cod_td" => $this->input->post("Cod_TD"),         
        );
        
        $dp_car_emp = array(
            "cod_b" => $this->input->post("Cod_B"),            
            "cod_s" => $this->input->post("Cod_S"),
            "cod_td" => $this->input->post("Cod_TD"),
            "cod_c" => $this->input->post("Cod_C"),
            "ficha_emp" => $this->input->post("Ficha"),
            "f_ini" => $this->input->post("F_Ini"),
            "f_fin" => $this->input->post("F_Fin"),
        );
        
        $datatel = array(            
            "ficha_emp" => $this->input->post("Ficha"),
            "num_telf" => $this->input->post("Telefono"),
        );
                
        $dataemail = array(            
            "ficha_emp" => $this->input->post("Ficha"),
            "dir_ce" => $this->input->post("Email"),
        );
        
        $data = array (
            "ficha" => $this->input->post("Ficha"),
            "nombre" => $this->input->post("Nombre"),
            "dir_emp" => $this->input->post("Dir_Emp"),
            "est_civil" => $this->input->post("Est_Civil"),
            "sexo" => $this->input->post("Sexo"),
            "f_nac" => $this->input->post("F_Nac"),
            "f_ing" => $this->input->post("F_Ing"),
            "f_egr" => $this->input->post("F_Egr"),
            "status" => $this->input->post("Status"),
            "s_dev" => $this->input->post("S_Dev"),
            "ficha_j" => $this->input->post("Ficha_J"),
            "cod_b" => $this->input->post("Cod_B"),            
            "cod_s" => $this->input->post("Cod_S"),
            "cod_td" => $this->input->post("Cod_TD"),  
            "cod_c" => $this->input->post("Cod_C"),
            "ficha_emp" => $this->input->post("Ficha"),
            "f_ini" => $this->input->post("F_Ini"),
            "f_fin" => $this->input->post("F_Fin"),
            "num_telf" => $this->input->post("Telefono"),
            "dir_ce" => $this->input->post("Email"),   
        );
        
        
        /*$data['ficha'] = '';
        $data['nombre'] = '';
        $data['dir_emp'] = '';
        $data['est_civil'] = '';
        $data['sexo'] = '';*/
        /*$data['f_nac'] = NULL;
        $data['f_ing'] = NULL;
        $data['f_egr'] = NULL;/*
        $data['status'] = '';
        $data['s_dev'] = '';
        $data['ficha_j'] = '';
        $data['cod_b'] = '';
        $data['cod_s'] = '';
        $data['cod_td'] = '';
        $data['cod_c'] = '';
        $data['ficha_emp'] = '';
        $data['f_ini'] = NULL;
        $data['f_fin'] = NULL;
        $data['num_telf'] = '';
        $data['dir_ce'] = '';*/
        
               
                
        $tipo_op = $this->input->post("Operacion");
         
        switch ($tipo_op) {
                    case "Insertar":{
                        $this->form_validation->set_error_delimiters('<span class="mensaje-error">', '</span>');
                            if ($this->form_validation->run('Empleado') === FALSE) {
                                $this->load->view('dbwindows/Empleado');
                            } else {
                                $this->load->model('EmpleadoDB');
                                $this->EmpleadoDB->Insert_Table($datae);
                                $this->EmpleadoDB->Insert_Table_Telf($datatel);
                                $this->EmpleadoDB->Insert_Table_Email($dataemail);
                                $this->EmpleadoDB->Insert_Table_Cargo($dp_car_emp);                                
                                $this->load->view('Clean/Empleado',$datae);                                
                            }
                    } 
                        break;
                    case "Actualizar":
                    {   
                    
                       $this->form_validation->set_error_delimiters('<span class="mensaje-error">', '</span>');
                            if ($this->form_validation->run('updEmpleado') === FALSE) {
                                $this->load->view('dbwindows/Empleado');
                            } else {                                   
                            $this->load->model('EmpleadoDB');
                            if ($this->EmpleadoDB->update_entryban($datae))
                                $this->load->view('Clean/Empleado');
                            else
                                echo "Codigo no coindice, vaya atras en su navegador";    
                            }
 
                    } break;
                    case "Eliminar":
                    {
                   
                    $borrar = array (
                    'eliminar' => $this->input->post("Ficha"),            
                      ); // NOMBRE DEL INPUT COD o Ficha
                    
                    
                     
                    $this->form_validation->set_error_delimiters('<span class="mensaje-error">', '</span>');
                            if ($this->form_validation->run('Eliminar') === FALSE) {
                                $this->load->view('dbwindows/Empleado');
                            } else {
                                $this->load->model('EmpleadoDB');     
                                    if ($this->EmpleadoDB->Delete_Table($borrar))
                                        $this->load->view('Clean/Empleado',$datae);
                                    else   {         
                                        echo "Ficha o Codigo inexistente en la relacion o tabla" ;
                                    ?>
                                    <br>
                                    <?php
                                        echo "Haga atras en su navegador para continuar"; 
                                    }
                            }                          
                    } 
                    break;
                    
                    
                        
                    default:
                        echo "nothing happens";
                }
        
        
    }

                 
}
